module.exports = function($scope, $state, $stateParams,$resource){
// alert('dksfja');
   var placeid = $stateParams.placeid;
    //  alert(placeid);
	$scope.placeid = $stateParams.placeid;
	$scope.id = $stateParams.placeid;
	$scope.searchform = {
		'selected' : {
			'name' : ''
		}
	}
	$scope.objt = {
		'name' : '',
		'placename' : '',
		'place_code' : '',
		'used_state' : '0',
		'state' : '1',
		'book_info' : ''
	};

	 function makeArr(str){
            var obj = [];

            if(str === undefined || str.length === 0) return obj;

            var arr = str.split(',');
            for(var i = 0; i < arr.length; i++)
            {
                obj.push({'name' : arr[i]});
            }
            return obj;
        };

    	function makeStr(arr){
            
            if(!angular.isArray(arr)) return '';

            var arr1 = [];
            for(var i = 0; i < arr.length; i++)
            {
                arr1.push(arr[i].name);
            }
            return arr1.join(',');
        };
	
	// $scope.midstart = new Date();
	// $scope.midend = new Date();

	// //有效区间
	// $scope.section = {};
	// $scope.section.start = {};
	// $scope.section.start.date = {};

	// $scope.section.end = {};
	// $scope.section.end.date = {};

	// $scope.today = function() {
	// 	$scope.section.start.date = $scope.section.end.date = new Date();
	// };
	// $scope.today();
	// $scope.open = function(obj) {
	// 	obj.opened = true;
	// };

	$scope.objs = [];

	$scope.change = function(code){
		// alert('111');
		console.log(code);
		$scope.objt.place_code = code;
		console.log($scope.objt.place_code);
	}
	$resource('/api/as/tc/placeview/jlist', {}, {}).
	save({},function(res) {	        
		if(res.errcode === 0)
		{
			$scope.viewarr = res.data;
			var array = res.data;
			//$scope.objt.place_code = res.data[0].code;
			// console.log(666666666666);
			console.log($scope.viewarr);
			console.log($scope.objt);
			console.log(6666666666);
			console.log($scope.viewarr);
			for(var i = 0; i < array.length; i++){
				if(placeid == array[i].code){
					console.log(array[i].code);
					$scope.objt.place_code = array[i].code;
					$scope.searchform.selected.name = array[i].name;
				}
			}
		}
		else
		{
			alert(res.errmsg);
		}
	});


	//保存按钮
	$scope.gogo = function(){

		if(!check()) return;
		// $scope.objt.place_code = $scope.searchform.selected.code;					
		var para = {
			// 'start_date' : getDate($scope.section.start.date),
			// 'end_date' : getDate($scope.section.end.date),
			'print_setup' : makeStr($scope.objs)
		}

		para = angular.extend($scope.objt, para);
		// para.start_date	 = getDate($scope.midstart);
		// para.end_date	 = getDate($scope.midend);
		console.log(parseInt(para.start_date));
		console.log(parseInt(para.end_date));
		// if ( parseInt(para.start_date.substring(0,4)+para.start_date.substring(5,7)+para.start_date.substring(8,10)) > parseInt(para.end_date.substring(0,4)+para.end_date.substring(5,7)+para.end_date.substring(8,10)) ) {
		// 	alert('有效时间的初始日期应早于末尾日期');
		// 	return;
		// }
		$resource('/api/as/tc/type2/create', {}, {}).		
		save(para, function(res){
			console.log('入参');
			console.log(para);

			console.log(res);

			if(res.errcode === 0)
			{
				alert('保存成功');
				$state.go('app.tkttype',{'placeid' : $scope.objt.place_code});
				// $state.go('app.edittkttype', {'id' : res.data.uuid});
			}
			else
			{
				alert(res.errmsg);
			}

		});

	};

	function check(){

		if($scope.objt.name === '')
		{
			alert('请输入票种名称');
			return false;
		}

		if($scope.objt.code === '')
		{
			alert('请输入票种编号');
			return false;
		}

		return true;
	}



	$scope.printadd = function(){
		var obj = {
			'name' : ''
		};
		$scope.objs.push(obj);
	};

	$scope.printdel = function(index){
		$scope.objs.splice(index,1); 
	};

	


};
